package com.simple.component;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.billy.cc.core.component.CC;
import com.billy.cc.core.component.CCResult;
import com.billy.cc.core.component.IComponentCallback;

/**
 * Copyright (C), 2016-2020,呼我出行网络科技有限公司
 * FileName: TestComponent
 * Author: Administrator
 * Date: 2020/11/4 10:33
 * Description:
 * History:
 */
public class ComponentHelper {

    public static boolean excute(CC cc) {
        boolean isAsync = false;
        if (!isAsync) {
            tongBuDiaoYong(cc);
            return false;
        } else {
            yiBuDiaoYongAsync(cc);
            return true;
        }
    }

    /**
     * 同步调用
     *
     * @param cc
     */
    public static void tongBuDiaoYong(CC cc) {
        CCResult result = CcCall.callComponent(
                ComponentAction.ACTION_LOGIN,
                ComponentAction.ACTION_LOGIN);
        CCResult ccResult;
        // 根据登录状态决定是否打开页面
        // 这里也可以添加更多的前置判断逻辑
        if (result.isSuccess()) {
            ccResult = CCResult.success();
            //已登录成功，打开目标页面
            startOrderListActivity(cc, result);
            //同步方式实现（在return之前听过CC.sendCCResult()返回组件调用结果），return false
        } else {
            //登录失败，返回失败信息
            ccResult = result;
        }
        CC.sendCCResult(cc.getCallId(), ccResult);

    }

    /**
     * 异步调用
     *
     * @param cc
     */
    public static void yiBuDiaoYongAsync(CC cc) {
        String result = CcCall.callComponentAsyncMain(
                ComponentAction.ACTION_LOGIN,
                ComponentAction.ACTION_LOGIN, false, new IComponentCallback() {

                    @Override
                    public void onResult(CC cc, CCResult result) {
                        CCResult ccResult;
                        // 根据登录状态决定是否打开页面
                        // 这里也可以添加更多的前置判断逻辑
                        if (result.isSuccess()) {
                            ccResult = CCResult.success();
                            //登录成功，打开目标页面
                            startOrderListActivity(cc, result);
                        } else {
                            //登录失败，返回失败信息
                            ccResult = result;
                        }
                        CC.sendCCResult(cc.getCallId(), ccResult);
                    }
                }, "data", cc.getParamItem("data"));
    }

    public static void startOrderListActivity(CC cc, CCResult result) {
        Context context = cc.getContext();
        Intent intent = new Intent(context, TargetActivity.class);
        intent.putExtra("data", (String) cc.getParamItem("data"));
        if (result != null) {
            intent.putExtra(Global.KEY_USERNAME, (String) result.getDataItem(Global.KEY_USERNAME));
        }
        if (!(context instanceof Activity)) {
            // context maybe an application object if caller dose not setContext
            // or call across apps
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }
}
